/*******************************************************************************
 * 文件名: BeanReflect.java
 * 描    述: 实体转换类
 * 创建人: Dean Huang(hudi-112@163.com)
 * 日　期: 2017-12-22
 * 描　述: 文件描述
 * 版　本: 1.0
 *******************************************************************************/

package com.z1.core.reflect;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

public class BeanReflect {

	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	/**
	 * 实体转换为键值集
	 * @param <T>
	 * @param t
	 * @return
	 */
	public static <T> Map<String, Object> getMap(T t) {
		if (null == t) {
			return null;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		Method[] methods = t.getClass().getMethods();
		for (Method method : methods) {
			String methodName = method.getName();
			if (methodName.startsWith("get")) {
				String fieldName = methodName.substring(3, 4).toLowerCase() + methodName.substring(4);
				Object obj = null;
				try {
					obj = method.invoke(t);
				} 
				catch (Exception e) {
					e.printStackTrace();
				}
				if (null == obj) {
					map.put(fieldName, null);
				}
				else {
					map.put(fieldName, obj.toString());
				}
			}
		}
		return map;
	}
	
	/**
	 * 键值集转换成实体
	 * @param <T>
	 * @param t
	 * @param map
	 */
	public static <T> void getT(T t, Map<String, Object> map) {
		if (null != map) {
			Field[] fields = t.getClass().getFields();
			for (Field field : fields) {
				try {
					String fieldName = field.getName();
					if (map.containsKey(fieldName)) {
						String methodName = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
						Method method = null;
						method = t.getClass().getMethod(methodName, field.getType());
						Object v = map.get(fieldName);
						if (field.getType().equals(java.lang.Integer.class)) {
							method.invoke(t, null == v ? null : (int)Double.parseDouble(v.toString()));
						}
						else if (field.getType().equals(java.util.Date.class)) {
							method.invoke(t, null == v ? null : sdf.parse(v.toString()));
						}
						else if (field.getType().equals(java.lang.Float.class)) {
							method.invoke(t, null == v ? null : Float.parseFloat(v.toString()));
						}
						else if (field.getType().equals(java.lang.Double.class)) {
							method.invoke(t, null == v ? null : Double.parseDouble(v.toString()));
						}
						else if (field.getType().equals(Object.class)) {
							method.invoke(t, v);
						}
						else {
							method.invoke(t, v);
						}
					}
				}
				catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	/**
	 * 从实体T中获取指定字段值
	 * @param <T> 
	 * @param t 
	 * @param fieldName 字段名称
	 * @return
	 */
	public static <T> Object getObjectFromT(T t, String fieldName) {
		if (null != t && null != fieldName) {
			String methodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
			Method method = null;
			try {
				method = t.getClass().getMethod(methodName);
				if (null != method) {
					return method.invoke(t);
				}
			}
			catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	/**
	 * 向实体T指定字段赋值
	 * @param <T>
	 * @param t
	 * @param fieldName 字段名称
	 * @param value 值
	 * @param paramType 字段类型
	 */
	public static <T> void setObjectToT(T t, String fieldName, Object value, Class<?> paramType) {
		if (null != t && null != fieldName) {
			if (null == paramType) {
				try {
					Field field = t.getClass().getField(fieldName);
					paramType = field.getType();
				}
				catch (Exception e) {
					e.printStackTrace();
				}
			}
			
			Method method = null;
			try {
				String methodName = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
				method = t.getClass().getMethod(methodName, paramType);
				if (null != method) {
					method.invoke(t, value);
				}
			}
			catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 实体、对象间参数赋值
	 * @param <T>
	 * @param t
	 * @param targetFieldName
	 * @param srcObj
	 * @param srcFieldName
	 */
	public static <T> void setObjectFromObjectToT(T t, String targetFieldName, Object srcObj, String srcFieldName) {
		if (null == srcFieldName) {
			srcFieldName = targetFieldName;
		}
		Object value = getObjectFromT(srcObj, srcFieldName);
		setObjectToT(t, targetFieldName, value, null);
	}
	
}
